package cn.project.engineering.ui.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import cn.project.engineering.R
import cn.project.engineering.WebUrlActivity
import cn.project.engineering.http.bean.MenuInfo
import cn.project.engineering.ui.DianjianActivity
import cn.project.engineering.ui.DistributionActivity
import cn.project.engineering.ui.MainActivity
import cn.project.engineering.utils.IpAddressUtils

/**
 * @author  lifuhai
 * @date 2021/2/21 18:44
 */
class MenuAdapter(val context: Context) : RecyclerView.Adapter<MenuAdapter.ViewHolder>() {
    var onVideoClickListener: (() -> Unit)? = null
    var flable: (() -> Unit)? = null
    var Onback: (() -> Unit)? = null

    private val menus: MutableList<MenuInfo> = ArrayList()

    fun setData(list: List<MenuInfo>) {
        menus.clear()
        menus.addAll(list)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_menu, parent, false))
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val menuInfo = menus[position]
        holder.menuName.text = menuInfo.MenuName
        holder.itemView.setOnClickListener {
//            if (0 == position) {
//                return@setOnClickListener
//            }

            if ("视频" == menuInfo.MenuName) {
                onVideoClickListener?.invoke()
            } else if("设备点检"==menuInfo.MenuName){
                val intent = Intent(context, DianjianActivity::class.java)
                context.startActivity(intent)
            }else if("文件分配"==menuInfo.MenuName){
                val intent = Intent(context, DistributionActivity::class.java)
                context.startActivity(intent)
            }else if("标签扫描"==menuInfo.MenuName){
                flable?.invoke()

            } else if("返回启动页面"==menuInfo.MenuName){
//                val intent = Intent(context, DianjianActivity::class.java)
//                context.startActivity(intent)
                Onback?.invoke()
            } else {
                val intent = Intent(context, WebUrlActivity::class.java)
                intent.putExtra("title", menuInfo.MenuName)
                intent.putExtra("url", "${menuInfo.WebUrl}?MacIP=${IpAddressUtils.getMacAddress()}")
                context.startActivity(intent)
            }
        }
    }

    override fun getItemCount(): Int {
        return menus.size
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val menuName: TextView = itemView.findViewById(R.id.textMenuName)
    }
}